﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DataGridViewAutoFilter;
using DocPlat.Library;

namespace DocPlat.UI
{
    public partial class ServicesList : UserControl
    {
        ServerObject mServerObject = null;
        bool mNewRecord = false;

        public ServicesList()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void FillServicesListGridView()
        {
            DataTable dsource = mServerObject.GetServicesList();
            BindingSource bindigSource = new BindingSource(dsource, null);
            servicesGridView.DataSource = bindigSource;

            if (servicesGridView.DataSource != null)
            {
                foreach (DataGridViewColumn col in servicesGridView.Columns)
                {
                    col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
                }

                servicesGridView.AutoResizeColumns();
            }
            if (dsource == null || dsource.Rows.Count == 0)
            { mNewRecord = true; }
        }

        private void ServicesList_Load(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(servicesGridView, 5);
            tableLayoutPanel2.Visible = false;

            FillServicesListGridView();

            idTextBox.Text = string.Empty;
            descriptionTextBox.Text = string.Empty;
            regimPrestTextBox.Text = "-";
            tipulAchitTextBox.Text = "-";
            costTextBox.Text = string.Empty;
        }

        private void editServicesList()
        {
            tableLayoutPanel1.SetColumnSpan(servicesGridView, 4);
            tableLayoutPanel2.Visible = true;

            if (servicesGridView.SelectedRows.Count == 1)
            {
                idTextBox.Text = servicesGridView.Rows[servicesGridView.SelectedRows[0].Index].Cells["id"].Value.ToString();
                descriptionTextBox.Text = servicesGridView.Rows[servicesGridView.SelectedRows[0].Index].Cells["Description"].Value.ToString();
                regimPrestTextBox.Text = servicesGridView.Rows[servicesGridView.SelectedRows[0].Index].Cells["RegimPrest"].Value.ToString();
                tipulAchitTextBox.Text = servicesGridView.Rows[servicesGridView.SelectedRows[0].Index].Cells["TipulAchit"].Value.ToString();
                costTextBox.Text = servicesGridView.Rows[servicesGridView.SelectedRows[0].Index].Cells["Cost"].Value.ToString();
            }            
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            editServicesList();
        }

        private void servicesGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            editServicesList();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(servicesGridView, 5);
            tableLayoutPanel2.Visible = false;

            mNewRecord = false;

            idTextBox.Text = string.Empty;
            descriptionTextBox.Text = string.Empty;
            regimPrestTextBox.Text = "-";
            tipulAchitTextBox.Text = "-";
            costTextBox.Text = string.Empty;
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(servicesGridView, 4);
            tableLayoutPanel2.Visible = true;

            mNewRecord = true;

            idTextBox.Text = string.Empty;
            descriptionTextBox.Text = string.Empty;
            regimPrestTextBox.Text = "-";
            tipulAchitTextBox.Text = "-";
            costTextBox.Text = string.Empty;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(servicesGridView, 5);
            tableLayoutPanel2.Visible = false;

            bool resultOperation = false;
            if (mNewRecord)
            {
                double cost = 0;
                double.TryParse(costTextBox.Text.Trim(), out cost);

                resultOperation = mServerObject.AddServices(descriptionTextBox.Text.Trim(), regimPrestTextBox.Text.Trim(), tipulAchitTextBox.Text.Trim(), cost);
            }
            else
            {
                int pructID = 0;
                if (servicesGridView.SelectedRows.Count == 1)
                {
                    int.TryParse(servicesGridView.Rows[servicesGridView.SelectedRows[0].Index].Cells["id"].Value.ToString(), out pructID);
                }
                double cost = 0;
                double.TryParse(costTextBox.Text.Trim(), out cost);

                if (pructID != 0)
                    resultOperation = mServerObject.UpdateServices(pructID, descriptionTextBox.Text.Trim(), regimPrestTextBox.Text.Trim(), tipulAchitTextBox.Text.Trim(), cost);
            }

            if (!resultOperation)            
            {
                MessageBox.Show("Failure savind data!");
            }

            FillServicesListGridView();
            idTextBox.Text = string.Empty;
            descriptionTextBox.Text = string.Empty;
            regimPrestTextBox.Text = "-";
            tipulAchitTextBox.Text = "-";
            costTextBox.Text = string.Empty;

            mNewRecord = false;
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            mServerObject.CloseThisTab(this);
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            FillServicesListGridView();
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure ???","Attention!",MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                int idserv = 0;
                int.TryParse(servicesGridView.Rows[servicesGridView.SelectedRows[0].Index].Cells["id"].Value.ToString(), out idserv);

                if (idserv == 0 || !mServerObject.DeleteServices(idserv))                
                {
                    MessageBox.Show("Error deleting record!");
                }

                FillServicesListGridView();

                idTextBox.Text = string.Empty;
                descriptionTextBox.Text = string.Empty;
                regimPrestTextBox.Text = "-";
                tipulAchitTextBox.Text = "-";
                costTextBox.Text = string.Empty;
            }
        }
    }
}
